package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// Terminal 终端，分95,97,98
type Terminal struct {
	ID                      string `json:"id" gorm:"column:id;primaryKey;type:bigint;size:64;comment:主键"`
	RobotID                 string `json:"robot_id" gorm:"column:robot_id;type:string;size:40;index:idx_robot_terminal,priority:1;comment:设备id"`
	TerminalType            string `json:"terminal_type" gorm:"column:terminal_type;type:string;size:20;index:idx_robot_terminal,priority:2;comment:客户端类型"`
	LatestActiveTime        int64  `json:"latest_active_time" gorm:"column:latest_active_time;type:int;size:64;comment:最后一次活跃时间"`
	CurrentVersion          string `json:"current_version" gorm:"column:current_version;type:string;size:50;comment:当前版本"`
	UpgradeStatus           int    `json:"upgrade_status" gorm:"column:upgrade_status;type:int;size:8;default:0;comment:升级状态,0-无动作，1-等待下载,2-暂不升级，3-下载中，4-下载完成，5-升级成功,6-升级失败,7-取消升级,8-升级中"`
	UpgradeDownloadProgress int    `json:"upgrade_download_progress" gorm:"column:upgrade_download_progress;type:int;size:8;default:0;comment:升级进度"`
	VolumePercent           int    `json:"volume_percent" gorm:"column:volume_percent;type:int;size:8;default:-1;comment:音量百分比"`
	Arch                    int    `json:"arch" gorm:"column:arch;type:int;size:8;comment:终端架构"`
	CreatedAt               int64  `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间"`
	UpdatedAt               int64  `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:false;type:int;size:64;comment:更新时间"`
}

// TableName 表名
func (Terminal) TableName() string {
	return "terminal"
}

// BeforeCreate 自增id
func (c *Terminal) BeforeCreate(tx *gorm.DB) (err error) {
	if c.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	c.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &Terminal{})
}
